package de.playmee;

import java.util.LinkedList;

public class Playlist {
	
	private long id;
	private long channelId;

	// the track ids are stored in a LinkedList
	private LinkedList<Long> track_ids;

	// currentPos points to the current track, or -1 if not started
	private int currentPos;

	// if the playlist should repeat
	private boolean repeat;

	public Playlist(long _channelId, boolean _repeat) {
		channelId = _channelId;
		currentPos = -1;
		repeat = _repeat;
		track_ids = new LinkedList<Long>();
	}
	
	/**
	 * sets the id
	 * 
	 * @param id
	 */
	public void setId(long id) {
		this.id = id;
	}
	
	/**
	 * @return id
	 */
	public long getId() {
		return id;
	}
	
	/**
	 * @return channelId
	 */
	public long getChannelId() {
		return channelId;
	}
	
	/**
	 * @return track_ids
	 */
	public LinkedList<Long> getTrackIds() {
		return track_ids;
	}

	/**
	 * gets the next track id
	 * 
	 * @return track id or null if there is no following track available
	 */
	public Long next() {
		if (track_ids.size() > 0) {
			if (currentPos + 1 < track_ids.size()) {
				return track_ids.get(++currentPos);
			}
			if (repeat) {
				currentPos = 0;
				return track_ids.get(currentPos);
			}
		}
		return null;
	}

	/**
	 * gets the previous track
	 * 
	 * @return track id or null if there is no previous track available
	 */
	public Long previous() {
		if (track_ids.size() > 0) {
			if (currentPos > 0) {
				return track_ids.get(--currentPos);
			}
			if (repeat) {
				currentPos = track_ids.size();
				return track_ids.get(currentPos);
			}
		}
		return null;
	}
	
	/**
	 * gets the track at a particular position
	 * 
	 * @param pos
	 * @return
	 */
	public Long get(int pos) {
		if (track_ids.size() > 0 && pos >= 0 && pos < track_ids.size()) {
			currentPos = pos;
			return track_ids.get(currentPos);
		}
		return null;
	}

	/**
	 * inserts the track _trackid at position _pos: if _pos is less than the size
	 * of the playlist else at the end
	 * 
	 * @param _pos
	 * @param _track
	 */
	public void addTrack(int _pos, long _trackid) {
		if (_pos >= 0 && _pos < track_ids.size())
			track_ids.add(_pos, _trackid);
		else
			appendTrack(_trackid);
	}

	/**
	 * inserts the track _trackid at the end
	 * 
	 * @param _trackid
	 */
	public void appendTrack(long _trackid) {
		track_ids.add(_trackid);
	}

	/**
	 * deletes the _pos'th track id (if existing)
	 * 
	 * @param _pos
	 */
	public void dropTrack(int _pos) {
		if (_pos >= 0 && _pos < track_ids.size())
			track_ids.remove(_pos);
	}

	/**
	 * deletes the current track id
	 */
	public void dropCurrentTrack() {
		dropTrack(currentPos);
	}

	/**
	 * @return the current track id
	 */
	public Long getCurrentTrack() {
		if (currentPos > -1) {
			return track_ids.get(currentPos);
		}
		return null;
	}
	
	/**
	 * @return currentPos
	 */
	public int getCurrentPos() {
		return currentPos;
	}
	
	/**
	 * creates a string with all track ids of the playlist
	 * 
	 * @return string
	 */
	public String getInfo() {
		if (track_ids.isEmpty()) {
			return "No music found for this channel.";
		}
		String s = "";
		for (long id : track_ids) {
			if (!s.isEmpty())
				s += ", ";
			s += id;
		}
		return "Music found: " + track_ids.size() + "\n\n" + s;
	}
}